﻿using ERP.ERPSystem.WarehouseManagement.Dto;
using ERP.Team.WarehouseManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ERP.ERPSystem.WarehouseManagement
{
    /// <summary>
    /// 仓储管理-库存盘点-xzc
    /// </summary>
    public class InventoryCountsServices:ApplicationService, IInventoryCountsServices
    {
        private readonly IRepository<InventoryCounts> _inventorycountsRepositroy;

        public InventoryCountsServices(IRepository<InventoryCounts> inventorycountsRepositroy)
        {
            _inventorycountsRepositroy = inventorycountsRepositroy;
        }
        /// <summary>
        /// 新增库存盘点
        /// </summary>
        /// <param name="inventorycountsDto"></param>
        /// <returns></returns>
        public async Task<int> AddInventoryCounts(InventoryCountsDto inventorycountsDto)
        {
            var i = ObjectMapper.Map<InventoryCountsDto,InventoryCounts>(inventorycountsDto);
            var inventoryCounts = await _inventorycountsRepositroy.InsertAsync(i);
            return inventoryCounts == null ? 0 : 1;
        }
        /// <summary>
        /// 更改库存盘点
        /// </summary>
        /// <param name="inventorycountsDto"></param>
        /// <returns></returns>
        public async Task<int> UpdateInventoryCounts(InventoryCountsDto inventorycountsDto)
        {
            var i = ObjectMapper.Map<InventoryCountsDto, InventoryCounts>(inventorycountsDto);
            var inventoryCounts = await _inventorycountsRepositroy.UpdateAsync(i);
            return inventoryCounts == null ? 0 : 1;
        }
        /// <summary>
        /// 通过id查询到相对应的库存盘点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<InventoryCounts>> FindInventoryCounts(int id)
        {
            var inventorycountsList = await _inventorycountsRepositroy.GetListAsync(m=>m.Id == id);
            return inventorycountsList;
        }
        /// <summary>
        /// 删除库存盘点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeletedInventoryCounts(int id)
        {
            await _inventorycountsRepositroy.DeleteAsync(m=>m.Id == id);
        }
        /// <summary>
        /// 查询库存盘点
        /// </summary>
        /// <param name="getinventorycountsdto"></param>
        /// <returns></returns>
        public async Task<List<InventoryCounts>> GetInventoryCounts(GetInventoryCountsDto getinventorycountsdto)
        {
            var inventorycountsList = await _inventorycountsRepositroy.GetListAsync();
            inventorycountsList = inventorycountsList.WhereIf(!string.IsNullOrEmpty(getinventorycountsdto.InventoryCode), m => m.InventoryCode == getinventorycountsdto.InventoryCode).ToList();
            inventorycountsList = inventorycountsList.WhereIf(!string.IsNullOrEmpty(getinventorycountsdto.States), m => m.States == getinventorycountsdto.States).ToList();
            inventorycountsList = inventorycountsList.WhereIf(!string.IsNullOrEmpty(getinventorycountsdto.InventoryName), m => m.InventoryName.Contains(getinventorycountsdto.InventoryName)).ToList();
            inventorycountsList = inventorycountsList.WhereIf(!string.IsNullOrEmpty(getinventorycountsdto.InventoryType), m => m.InventoryType.Contains(getinventorycountsdto.InventoryType)).ToList();
            inventorycountsList = inventorycountsList.WhereIf(!string.IsNullOrEmpty(getinventorycountsdto.WarehouseName), m => m.WarehouseName.Contains(getinventorycountsdto.WarehouseName)).ToList();
            inventorycountsList = inventorycountsList.WhereIf(!string.IsNullOrEmpty(getinventorycountsdto.InventoryDate.ToString()), m => m.InventoryDate == getinventorycountsdto.InventoryDate).ToList();
            return inventorycountsList;
        }
    }
}
